package com.powernode.service.Impl;

import com.powernode.cache.CacheService;
import com.powernode.common.Page;
import com.powernode.mapper.SysDictDataMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.powernode.domain.SysDictType;
import com.powernode.mapper.SysDictTypeMapper;
import com.powernode.service.SysDictTypeService;

import java.util.List;

@Service
public class SysDictTypeServiceImpl extends BaseServiceImpl implements SysDictTypeService{



    @Resource
    private SysDictTypeMapper sysDictTypeMapper;

    @Resource
    private CacheService cacheService;

    @Resource
    private SysDictDataMapper sysDictDataMapper;


    @Override
    public int deleteByPrimaryKey(Long dictId) {
        cacheService.clear();
        //根据dictId查询字典类型
        SysDictType sysDictType = this.sysDictTypeMapper.selectByPrimaryKey(dictId);
        if(null!=sysDictType){
            this.sysDictDataMapper.deleteDictDataByDictType(sysDictType.getDictType());
        }
        return sysDictTypeMapper.deleteByPrimaryKey(dictId);
    }

    @Override
    public int insert(SysDictType record) {
        cacheService.clear();
        return sysDictTypeMapper.insert(record);
    }

    @Override
    public int insertSelective(SysDictType record) {
        cacheService.clear();
        return sysDictTypeMapper.insertSelective(record);
    }

    @Override
    public SysDictType selectByPrimaryKey(Long dictId) {
        cacheService.clear();
        return sysDictTypeMapper.selectByPrimaryKey(dictId);
    }

    @Override
    public int updateByPrimaryKeySelective(SysDictType record) {
        cacheService.clear();
        return sysDictTypeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SysDictType record) {
        cacheService.clear();
        return sysDictTypeMapper.updateByPrimaryKey(record);
    }


    /**
     * 分页查询字典类型
     * @param page
     * @return
     */
    @Override
    public Page<SysDictType> getByPage(Page<SysDictType> page) {
        com.github.pagehelper.Page startPage = startPage(page.getPageNumber(), page.getPageSize());
        List<SysDictType> sysDictTypes =sysDictTypeMapper.queryAllDictType(page.getParams());
        page.setList(sysDictTypes);
        page.setTotalCount(startPage.getTotal());
        return page;
    }

    @Override
    public void refreshCache() {
        cacheService.refreshCache();
    }

}
